package com.woniuxy.principal.controller;

import com.woniuxy.commons.anno.HasRole;
import com.woniuxy.commons.entity.AllTypeEnroll;
import com.woniuxy.commons.entity.ResponseEntity;
import com.woniuxy.commons.entity.wyh.GradeInfo;
import com.woniuxy.commons.entity.wyh.TeacherInfo;
import com.woniuxy.principal.service.EnrolmentRateService;
import com.woniuxy.principal.service.GradeDeanService;
import com.woniuxy.principal.service.SchoolLeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author： 雷鹏
 * Date: 2020/7/28 15:53
 */
@RestController
@RequestMapping("/gradeDean")
public class GradeDeanController {

    @Autowired
    private EnrolmentRateService enrolmentRateService;

    @Autowired
    private RedisTemplate<String, Object> redisTemlate;

    @Autowired
    private SchoolLeaderService schoolLeaderService;

    @Autowired
    private GradeDeanService gradeDeanService;

    @HasRole("principal")
    @GetMapping("/allTeacher")
    public ResponseEntity<List<TeacherInfo>> getAllTeacher(HttpServletRequest request) {

        String refreshToken = request.getHeader("refreshToken");
        Map<Object, Object> data = redisTemlate.opsForHash().entries(refreshToken);
        int rid = (int) data.get("rid");
        //从数据库查出登陆账号所在学校id
        int sid = schoolLeaderService.getSid(rid);

        List<TeacherInfo> allTeacher = gradeDeanService.getAllTeacher(sid);

        ResponseEntity<List<TeacherInfo>> responseEntity = new ResponseEntity<>();
        responseEntity.setCode(200);
        responseEntity.setMsg("成功");
        responseEntity.setData(allTeacher);

        return responseEntity;

    }

    @HasRole("principal")
    @GetMapping("/allGrade")
    public ResponseEntity<List<GradeInfo>> getAllGrade(HttpServletRequest request) {

        String refreshToken = request.getHeader("refreshToken");
        Map<Object, Object> data = redisTemlate.opsForHash().entries(refreshToken);
        int rid = (int) data.get("rid");
        //从数据库查出登陆账号所在学校id
        int sid = schoolLeaderService.getSid(rid);

        List<GradeInfo> allTeacher = gradeDeanService.getAllGrade(sid);

        ResponseEntity<List<GradeInfo>> responseEntity = new ResponseEntity<>();
        responseEntity.setCode(200);
        responseEntity.setMsg("成功");
        responseEntity.setData(allTeacher);

        return responseEntity;

    }


    @HasRole("principal")
    @PostMapping("/setGradeDean")
    public ResponseEntity<Void> setGradeDean(Integer gaId, Integer tcId) {

        System.out.println(gaId);
        System.out.println(tcId);
        gradeDeanService.setGradeDean(gaId, tcId);

        ResponseEntity<Void> responseEntity = new ResponseEntity<>();
        responseEntity.setCode(200);
        responseEntity.setMsg("成功");
        return responseEntity;

    }

}
